//
//  XMMyAddressView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/29.
//

#import "XMMyAddressView.h"
#import "XMMyAddressCell.h"
#import "XMAddressAddView.h"

@interface XMMyAddressView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UIView *noDataView;

@property (nonatomic, strong) XMAddressAddView *addressAddView;

@property (nonatomic, strong) NSArray *addressModelArr;

@end

@implementation XMMyAddressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    self.navTitleLabel.text = @"我的地址";
    [self addSubview:self.tbl];
    [self addSubview:self.noDataView];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, self.height - KScaleWidth(70.0) - KScaleWidth(44.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"新增见面地点" titleColor:kColorHEX(0x333333)];
    [addBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [addBtn addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setCornerRadius:addBtn.height/2];
    [self addSubview:addBtn];
}

//添加地址
- (void)addAction:(UIButton *)button {
    [self addSubview:self.addressAddView];
    [_addressAddView showWithAddressModel:nil];
}

//地址列表
- (void)setContentWithAddressModelArr:(NSArray *)addressModelArr {
    _addressModelArr = addressModelArr;
    if (!addressModelArr || addressModelArr.count == 0) {
        _noDataView.hidden = NO;
        _tbl.hidden = YES;
    }
    else {
        _noDataView.hidden = YES;
        _tbl.hidden = NO;
        [_tbl reloadData];
    }
}

//地区选择结果
- (void)setContentWithAreaStr:(NSString *)areaStr areaId:(NSString *)areaId {
    [_addressAddView setAreaSelectResult:areaStr areaId:areaId];
}

//地址添加成功
- (void)addressAddSuccess {
    [_addressAddView addressAddSuccess];
}

#pragma mark - Init

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _addressModelArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, KScaleWidth(8.0))];
    footView.backgroundColor = kColorHEX(0xF0F0F0);
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(118.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMMyAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMyAddressCellName"];
    XMAddressModel *addressModel = _addressModelArr[indexPath.section];
    [cell setContentWithAddressModel:addressModel];
    XMWeakSelf;
    cell.setDefaultBlock = ^{ //设置默认地址
        addressModel.defaultStatus = true;
        addressModel.isCreate = NO;
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(addressAddWithAddressModel:)]) {
            [weakSelf.delegate addressAddWithAddressModel:addressModel];
        }
//        [weakSelf.addressAddView showWithAddressModel:addressModel];
    };
    cell.editBlock = ^{  //地址编辑
        [weakSelf addSubview:weakSelf.addressAddView];
        [weakSelf.addressAddView showWithAddressModel:addressModel];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(addressSelectWithAddressModel:)]) {
        XMAddressModel *addressModel = _addressModelArr[indexPath.section];
        [self.delegate addressSelectWithAddressModel:addressModel];
    }
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH - KScaleWidth(80.0) - KScaleWidth(44.0))];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.backgroundColor = kColorHEX(0xffffff);//kColorHEX(0xF0F0F0);
        [_tbl registerClass:[XMMyAddressCell class] forCellReuseIdentifier:@"XMMyAddressCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (XMAddressAddView *)addressAddView {
    if (!_addressAddView) {
        _addressAddView = [[XMAddressAddView alloc] initWithFrame:self.bounds];
        XMWeakSelf; //创建地址
        _addressAddView.addAddressBlock = ^(XMAddressModel * _Nonnull addressModel) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(addressAddWithAddressModel:)]) {
                [weakSelf.delegate addressAddWithAddressModel:addressModel];
            }
        };
        _addressAddView.areaSelectBlock = ^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(areaSelect)]) {
                [weakSelf.delegate areaSelect];
            }
        };
    }
    return _addressAddView;
}

- (UIView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _noDataView.backgroundColor = kColorHEX(0xF5F5F5);
        
        UIImageView *noDataIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height/3 - KScaleWidth(172.0), self.width, KScaleWidth(172.0))];
        noDataIcon.contentMode = UIViewContentModeScaleAspectFit;
        noDataIcon.image = ImageNamed(@"mine_address_noData_icon");
        [_noDataView addSubview:noDataIcon];
        
        UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, noDataIcon.maxY + KScaleWidth(20.0), self.width, KScaleWidth(28.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentCenter];
        noDataLabel.text = @"暂无见面地点";
        [_noDataView addSubview:noDataLabel];
    }
    return _noDataView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
